package com.fengniu.ejb.business.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.fengniu.ejb.business.ICategoryBusinessLocal;
import com.fengniu.ejb.business.ICategoryBusinessRemote;
import com.fengniu.ejb.dao.CategoryDaoLocal;
import com.fengniu.ejb.dao.ContentDaoLocal;
import com.fengniu.ejb.dao.SiteDaoLocal;
import com.fengniu.ejb.dao.UserDaoLocal;
import com.fengniu.ejb.dao.util.IEJBConstants;
import com.fengniu.ejb.entity.Category;
import com.fengniu.ejb.entity.Category2Category;
import com.tianbing.adlottery.global.IConstants;

@Stateless
@LocalBinding(jndiBinding = IEJBConstants.CategoryBusinessLocalJndiName)
@RemoteBinding(jndiBinding = IEJBConstants.CategoryBusinessRemoteJndiName)
public class CategoryBusinessImpl implements ICategoryBusinessLocal, ICategoryBusinessRemote {
	@EJB(name = IEJBConstants.UserDaoLocalJndiName)
	private UserDaoLocal userDao;

	@EJB(name = IEJBConstants.SiteDaoLocalJndiName)
	private SiteDaoLocal siteDao;

	@EJB(name = IEJBConstants.ContentDaoLocalJndiName)
	private ContentDaoLocal contentDao;

	@EJB(name = IEJBConstants.CategoryDaoLocalJndiName)
	private CategoryDaoLocal categoryDao;

	public List<Category2Category> getListCategoryRelatedBrand(String categoryId) {
		Integer[] relationIds = { IConstants.RelationID.G2G_IS_BRAND_OF.getId() };
		List<Category2Category> g2gs = categoryDao.getListCategory2CategoryBy(null, relationIds, categoryId, null,
				null, 0, 100);
		return g2gs;
	}

	public Map<String, Object> getCategoryProducts(String contentCategoryId, String contentBrandCategoryId, int pgno,
			int pgsize) {
		Map<String, Object> result = new HashMap<String, Object>();
		Integer[] relationIds = { IConstants.RelationID.C2G_IS_MEMBER_OF.getId() };
		Integer[] contentTypeIds = { IConstants.Type.CONTENT_PRODUCT.getId() };
		// get list of content2category
		List content2CategoryList = contentDao.getListContent2CategoryBy(null, relationIds, contentCategoryId,
				contentBrandCategoryId, null, contentTypeIds, pgno, pgsize, "c2g.content.integral DESC");
		Long totalCount = contentDao.getCountContent2CategoryBy(null, relationIds, contentCategoryId,
				contentBrandCategoryId, null, contentTypeIds);

		// get category by gid
		Category contentCategory = categoryDao.findCategoryByCategoryId(contentCategoryId);
		List primaryContentCategoryTrail = categoryDao.getListCategoryPrimaryTrailBy(contentCategoryId);
		// get child categories of gid
		List<Category> childContentCategoryList = categoryDao.getListChildCategoryBy(contentCategoryId, 0, 100, null);

		result.put("content2CategoryList", content2CategoryList);
		result.put("totalCount", totalCount);
		result.put("contentCategory", contentCategory);
		result.put("primaryContentCategoryTrail", primaryContentCategoryTrail);
		result.put("childContentCategoryList", childContentCategoryList);
		// get brand category by brdgid
		if (contentBrandCategoryId != null) {
			Category contentBrandCategory = categoryDao.findCategoryByCategoryId(contentBrandCategoryId);
			result.put("brandCategory", contentBrandCategory);
		}

		return result;
	}

	public Map<String, Object> getCategoryStores(String storeCategoryId, String storeBizCategoryId, int pgno, int pgsize) {
		Map<String, Object> result = new HashMap<String, Object>();

		Integer[] relationIds = { IConstants.RelationID.S2G_IS_MEMBER_OF.getId() };

		// get list of site2Category
		Integer[] siteTypeIds = { IConstants.Type.SITE_STORE.getId() };
		List site2CategoryList = siteDao.getListSite2CategoryBy(null, relationIds, storeCategoryId, storeBizCategoryId,
				null, siteTypeIds, pgno, pgsize, "s2g.site.integral DESC");
		Long totalCount = siteDao.getCountSite2CategoryBy(null, relationIds, storeCategoryId, storeBizCategoryId, null,
				siteTypeIds);
		result.put("site2CategoryList", site2CategoryList);
		result.put("totalCount", totalCount);

		if (storeCategoryId != null && storeCategoryId.trim() != "") {
			// get category by gid
			Category siteCategory = categoryDao.findCategoryByCategoryId(storeCategoryId);
			List primarySiteCategoryTrail = categoryDao.getListCategoryPrimaryTrailBy(storeCategoryId);
			// get child categories of gid
			List<Category> childSiteCategoryList = categoryDao.getListChildCategoryBy(storeCategoryId, 0, 100, null);
			result.put("siteCategory", siteCategory);
			result.put("childSiteCategoryList", childSiteCategoryList);
			result.put("primarySiteCategoryTrail", primarySiteCategoryTrail);
		}

		if (storeBizCategoryId != null && storeBizCategoryId.trim() != "") {
			// get category by gid
			Category businessCategory = categoryDao.findCategoryByCategoryId(storeBizCategoryId);
			result.put("businessCategory", businessCategory);
		}

		return result;
	}

	public Map<String, Object> getCategoryHordes(String hordeCategoryId, String hordeBizCategoryId, int pgno, int pgsize) {
		Map<String, Object> result = new HashMap<String, Object>();

		Integer[] relationIds = { IConstants.RelationID.S2G_IS_MEMBER_OF.getId() };

		// get list of site2Category
		Integer[] siteTypeIds = { IConstants.Type.SITE_HORDE.getId() };
		List site2CategoryList = siteDao.getListSite2CategoryBy(null, relationIds, hordeCategoryId, hordeBizCategoryId,
				null, siteTypeIds, pgno, pgsize, "s2g.site.integral DESC");
		Long totalCount = siteDao.getCountSite2CategoryBy(null, relationIds, hordeCategoryId, hordeBizCategoryId, null,
				siteTypeIds);
		// get category by gid
		Category siteCategory = categoryDao.findCategoryByCategoryId(hordeCategoryId);
		List primarySiteCategoryTrail = categoryDao.getListCategoryPrimaryTrailBy(hordeCategoryId);
		// get child categories of gid
		List<Category> childSiteCategoryList = categoryDao.getListChildCategoryBy(hordeCategoryId, 0, 100, null);

		result.put("site2CategoryList", site2CategoryList);
		result.put("totalCount", totalCount);
		result.put("siteCategory", siteCategory);
		result.put("childSiteCategoryList", childSiteCategoryList);
		result.put("primarySiteCategoryTrail", primarySiteCategoryTrail);
		return result;
	}

	public Map<String, Object> getCategoryWebSites(String webSiteCategoryId, String webSiteBizCategoryId, int pgno,
			int pgsize) {
		Map<String, Object> result = new HashMap<String, Object>();

		Integer[] relationIds = { IConstants.RelationID.S2G_IS_MEMBER_OF.getId() };

		// get list of site2Category
		Integer[] siteTypeIds = { IConstants.Type.SITE_WEBSITE.getId() };
		List site2CategoryList = siteDao.getListSite2CategoryBy(null, relationIds, webSiteCategoryId,
				webSiteBizCategoryId, null, siteTypeIds, pgno, pgsize, "s2g.site.integral DESC");
		Long totalCount = siteDao.getCountSite2CategoryBy(null, relationIds, webSiteCategoryId, webSiteBizCategoryId,
				null, siteTypeIds);
		// get category by gid
		Category siteCategory = categoryDao.findCategoryByCategoryId(webSiteCategoryId);
		List primarySiteCategoryTrail = categoryDao.getListCategoryPrimaryTrailBy(webSiteCategoryId);
		// get child categories of gid
		List<Category> childSiteCategoryList = categoryDao.getListChildCategoryBy(webSiteCategoryId, 0, 100, null);

		result.put("site2CategoryList", site2CategoryList);
		result.put("totalCount", totalCount);
		result.put("siteCategory", siteCategory);
		result.put("childSiteCategoryList", childSiteCategoryList);
		result.put("primarySiteCategoryTrail", primarySiteCategoryTrail);
		return result;
	}

}
